package top.guxx.springframework.core.io;

import cn.hutool.core.lang.Assert;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @Author：Guxx
 * @Description：包装三种不同类型的资源处理方式
 * @Date：2024/6/9 21:34
 * @Filename：DefaultResourceLoader
 */
public class DefaultResourceLoader implements ResourceLoader{
    @Override
    public Resource getResource(String location) {
        Assert.notNull(location,"Location must be not null!");
        //判断是否为ClassPath文件
        if (location.startsWith(CLASSPATH_URL_PREFIX)){
            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
        }else {
            //判断是否为URL文件，如果抛异常说明不是，捕获异常返回包装系统文件
            try {
                URL url = new URL(location);
                return new UrlResource(url);
            } catch (MalformedURLException e) {
                return new FileSystemResource(location);
            }
        }
    }
}
